package team.gl.nio.cln;

import java.awt.Color;
import java.text.SimpleDateFormat;
import java.util.Vector;
import java.util.concurrent.TimeUnit;

import DXQclient.DXQclientManager;
import HGclient.GameDetails;
import P8.StoneAgeClient;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import team.gl.nio.cmn.Bag;

public class ClientHandler extends ChannelInboundHandlerAdapter {  
	
	long lastreconnecttime = 0;
	
	
	
	public static ChannelHandlerContext ctx_s = null;	

    @Override  
    public void channelRead(ChannelHandlerContext ctx, Object msg)  
            throws Exception {     
    	Bag us = (Bag)msg;
    	
    	SimpleDateFormat dfSec = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	
    	if(us.getReq().equals("bet")){
    		    		
//    		String[] params = us.getContents().split(",");
//    		
//    		boolean betres = StoneAgeClient.dxqClientManager.hgbet.betPankou(params[0], params[1], params[2],params[3], Integer.parseInt(params[4]));
//    		
//    		Bag res = new Bag("betres");
//    		
//    		String contents = Boolean.toString(betres);
//    		res.setContents(contents);
//    		
//    		NettyClient.sendRequest(res);
//    		System.out.println("bet message--------------------------------------");
//    		String[] params = us.getContents().split(",");
//    		Vector<DXQclientManager> managers = StoneAgeClient.betAccountsListWindow.getManagers();
//    		
//    		for(DXQclientManager manager : managers) {
//    			if(manager.getOnline()) {
//    				manager.hgbet.betPankou(params[0],
//    						params[1], params[2],params[3], Integer.parseInt(params[4]));
//    			}
//    		}
    	}else if(us.getReq().equals("gamedetails")){
    		
    		Vector<GameDetails> vecs =  us.getGamedetails();
    		
    		
    		
    		StoneAgeClient.gameDetailsWnd.updateGameDetailsVec(us.getGamedetails());
    		
    		StoneAgeClient.gameDetailsWnd.setStateText(us.updateTime);
    		
    		if(us.grabstat == false){
    			StoneAgeClient.gameDetailsWnd.setStateColor(Color.red);
    		}else{
    			StoneAgeClient.gameDetailsWnd.setStateColor(Color.GREEN);
    		}
    		
    	}else if(us.getReq().equals("pdataRatio")){
    		StoneAgeClient.gamepDetailsWnd.updateGameDetailsVec(us.gamepdetailsVec);
    	}else if(us.getReq().equals("dxqgames")){
    		
    		Vector<DXQclientManager> managers = StoneAgeClient.betAccountsListWindow.getManagers();
    		
    		for(DXQclientManager manager : managers) {
    			if(manager.getOnline()) {
    				manager.setDXQstatetxt(us.updateTime);
    			}
    		}
    		
    		
    		
    		
    		if(us.grabstat == false){
    			for(DXQclientManager manager : managers) {
    				if(manager.getOnline()) {
		    			manager.setDXQstatecolor(Color.red);
		    			manager.setDXQgrabstate(false);
    				}
    			}
    		}else{
    			
    			
    			System.out.println(dfSec.format(System.currentTimeMillis()) + "成功获取数据时间：" + us.updateTime);
    			
    			
    			if(System.currentTimeMillis() - dfSec.parse(us.updateTime).getTime() > 15*1000 && false){
    				if(ctx_s != null) {
    	        		ctx_s.close();
    	        		
    	        		lastreconnecttime = System.currentTimeMillis();
    	        		
    	        		System.out.println("与服务器数据相差时间间隔过大，强制进行重新连接");
    	        		
    	        		
    	        		
    	        		return;
    	        	}
    				
    			}
    			
    			
    			
    			for(DXQclientManager manager : managers) {
    				if(manager.getBet()) {
		    			manager.setDXQgrabstate(true);
		    			manager.setDXQstatecolor(Color.GREEN);
		    			manager.updateDXQ(us.dxqgames);
    				}
    			}
    			
    			
    			//统一显示betlist
    			//StoneAgeClient.betlistWnd.updateBetlistVec();
    			
    			
    		}
    		
    		
    	}
    	
       //System.out.println(us);         
    }  
    
    
    
    
    
	@Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("--- Server is active ---");
        ctx_s = ctx;
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("--- Server is inactive ---");
        NettyClient.channel = null;
        // 10s 之后尝试重新连接服务器
        System.out.println("2s 之后尝试重新连接服务器...");
        Vector<DXQclientManager> managers = StoneAgeClient.betAccountsListWindow.getManagers();
       
        for(DXQclientManager manager : managers) {
        	if(manager.getOnline()) {
	        	manager.setDXQstatetxt("失去连接,重新连接中...");	        
	        	manager.setDXQstatecolor(Color.red);
	        	manager.setDXQgrabstate(false);
        	}
        }
        
        
        NettyClient.group.schedule(new Runnable() {  
            
            @Override  
          
            public void run() {  
                while(NettyClient.connect() == false) {
                	
                	try{  					
    					Thread.sleep(5000);
    					
    				}catch(Exception e){
    					e.printStackTrace();
    				} 
                }
                
                Vector<DXQclientManager> managers = StoneAgeClient.betAccountsListWindow.getManagers();
                for(DXQclientManager manager : managers) {
                	if(manager.getOnline()) {
                		manager.setDXQstatecolor(Color.GREEN);
                	}
                }
                System.out.println("重新连接成功");
            }  
          
          }, 200L, TimeUnit.MILLISECONDS);  
          
        super.channelInactive(ctx);
        
        
        
       // NettyClient.sendRequest(new Bag("request"));
    }
    
    
    
    
    
    
    

}